package com.jc.util;

import java.io.Serializable;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;


public class Result<T> implements Serializable, IR<T> {

    private static final long serialVersionUID = 607220381713446629L;

    public static int SUCCESS_CODE = 200;
    public static int SYS_ERR_CODE = 500;
    public static String SUCCESS_MSG = "成功";
    public static String SYS_ERR_MSG = "系统异常";

    /**
     * 成功标记
     */
    private Boolean success;

    private Integer code;

    private String message;

    private T content;

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }

    @Override
    public Result<T> ok() {
        this.success = true;
        this.code = SUCCESS_CODE;
        this.message = SUCCESS_MSG;
        return this;
    }

    @Override
    public Result<T> ok(T t) {
        this.ok();
        this.content = t;
        return this;
    }

    @Override
    public Result<T> error(String message) {
        this.success = false;
        this.code = SYS_ERR_CODE;
        this.message = message;
        return this;
    }

    @Override
    public Result<T> error(Integer code, String message) {

        this.success = false;
        this.code = code;
        this.message = message;
        return this;
    }

    @Override
    public Result<T> error(String message, T t) {
        this.success = false;
        this.code = SYS_ERR_CODE;
        this.message = message;
        this.content = t;
        return this;
    }

    @Override
    public Result<T> error() {
        this.success = false;
        this.code = SYS_ERR_CODE;
        this.message = SYS_ERR_MSG;
        return this;
    }

    @Override
    public void setRCode(IRCode code) {
        if (code.getCode().equals(200)) {
            this.success = true;
        }
        this.code = code.getCode();
        this.message = code.getMsg();
    }

    @Override
    public void setRCode(IRCode code, T t) {
        this.setRCode(code);
        this.content = t;
    }

    @Override
    public void setRCode(boolean success, IRCode code, T t) {
        this.setRCode(code);
        this.success = success;
        this.content = t;
    }

    @Override
    public void setRCodeWithMsgSuffix(IRCode code, Object msgSuffix) {
        if (code.getCode().equals(200)) {
            this.success = true;
        }
        this.code = code.getCode();
        this.message = code.getMsg() + msgSuffix.toString();
    }

    public static <T> Result<T> create() {
        return new Result<T>();
    }



    public static <T> Result<T> create(T t) {
        Result<T> r = Result.create();
        r.ok(t);
        return r;
    }

    public static <T> Result<T> create(T t, int code, String message) {
        Result<T> r = Result.create();
        r.setSuccess(true);
        r.setContent(t);
        r.setCode(code);
        r.setMessage(message);
        return r;
    }


    /**
     * 处理 错误信息
     * @param r
     */
    public void errorMessage(Result r) {
        if (!r.getSuccess()){
            List<String> errorList = (List<String>) r.getContent();
            if (errorList!=null && errorList.size()>0){
                StringBuilder sb = new StringBuilder();
                for (int j = 0; j < errorList.size(); j++) {
                    sb.append(errorList.get(j) + "\r\n");
                }
                // 分割 errMsg 并去重
                String[] errorMessages = sb.toString().split("\r\n");
                Set<String> uniqueMessages = new LinkedHashSet<>(Arrays.asList(errorMessages));
                String filteredMessage = String.join("\r\n", uniqueMessages);
                r.setMessage(filteredMessage);
            }
        }
    }


}
